<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015－03－21 http://www.cencn.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 林群伟  <56583965@qq.com> <http://www.xzqly.com>
// +----------------------------------------------------------------------

namespace Admin\Model;
use Think\Model;

/**
 * 用户模型
 * @author 林群伟
 */

class MemberModel extends Model {

    protected $_validate = array(
      array('nickname', '1,16', '昵称长度为1-16个字符', self::EXISTS_VALIDATE, 'length')
      //array('nickname', '', '昵称被占用', self::EXISTS_VALIDATE, 'unique'), //用户名被占用
    );
    
    protected $_auto = array(
		array('birthday', 'strtotime', self::MODEL_BOTH, 'function'),
		array('reg_ip', 'get_client_ip', self::MODEL_INSERT, 'function', 1),
                array('reg_time', NOW_TIME, self::MODEL_INSERT),
		array('status', 'getStatus', self::MODEL_BOTH, 'callback')
     );

    public function lists($status = 1, $order = 'uid DESC', $field = true){
       
        $map = array('status' => $status);
        return $this->field($field)->where($map)->order($order)->select();
    }

    /**
     * 登录指定用户
     * @param  integer $uid 用户ID
     * @return boolean      ture-登录成功，false-登录失败
     */
    public function login($uid){
        
        /* 检测是否在当前应用注册 */
        $user = $this->field(true)->find($uid);
    
        if(!$user ) {
            $this->error = '用户不存在！'; //应用级别禁用
            return false;
        }
        if($user["status"]==0){
            $this->error= '用户已被禁用';
            return false;
        }
        if($user["status"]==-1){
            $this->error= '用户已被删除';
            return false;
        }
          
        
       
        $this->autoLogin($user);
        return true;
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout(){
        session('lqwsys_auth', null);
        session('lqwsys_auth_sign', null);
    }

    /**
     * 自动登录用户
     * @param  integer $user 用户信息数组
     */
    private function autoLogin($user){
        /* 更新登录信息 */
        $data = array(
            'uid'             => $user['uid'],
            'login'           => array('exp', '`login`+1'),
            'last_login_time' => NOW_TIME,
            'last_login_ip'   => get_client_ip(1),
        );
        $this->save($data);

        /* 记录登录SESSION和COOKIES */
        $auth = array(
            'uid'             => $user['uid'],
            'username'        => $user['nickname'],
            'last_login_time' => $user['last_login_time'],
            'select_type' => $user['select_type'],
            'logo'=>$user['logo'],
            'status' => $user['status'],
          
            
        );

        session('lqwsys_auth', $auth);
        session('lqwsys_auth_sign', data_auth_sign($auth));

    }

    public function getNickName($uid){
        
        return $this->where(array('uid'=>(int)$uid))->getField('nickname');
    }
    public function getStatus(){
        return TRUE;
    }

}
